<template>
	<el-drawer title="投票详情" v-model="visible" :size="800" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main>
				<el-form>
					<div style="text-align: center;font-weight: 600;font-size: 18px">{{form.title}}</div>
					<el-row :gutter="24" style="margin: 15px">
						<el-col :lg="8">
							<el-tag size="small" type="success">{{form.voteTypeName}}</el-tag>
						</el-col>
						<el-col :lg="8" style="text-align: center">
							{{form.beginTime}}~{{form.endTime}}
						</el-col>
						<el-col :lg="8" style="text-align: right">
							<el-tag size="small" type="info">{{form.sendDeptName}}</el-tag>
						</el-col>
					</el-row>
						<div v-html="form.content" style="margin: 15px;font-size: 14px"></div>
					<el-form-item label="相关附件" v-if="form.attachId!='' && form.attachId!=undefined">
						<attach-menu :attachIds="form.attachId" :roleFlag="3" :preview="true"></attach-menu>
					</el-form-item>
				<itemRecord :listData="itemJson"></itemRecord>
				</el-form>
				<el-footer style="text-align: center;">
					<el-button type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
				</el-footer>
			</el-main>
		</el-container>
	</el-drawer>
</template>

<script>
import AttachMenu from '@/views/module/attachMenu'
import itemRecord from './itemRecord'
export default {
	emits: ['success', 'closed'],
	components: {
		AttachMenu,
		itemRecord
	},
	data() {
		return {
			loading: false,
			itemJson:[],
			form: {
				voteId:"",
				title:"",
				voteTypeName:"",
				beginTime:"",
				endTime:"",
				content:"",
				attachId:"",
				itemJson:[]
			},
			visible: false,
			isSaveing: false,
		}
	},
	mounted() {
	},
	methods: {
		async submit() {
			let resJson=[];
			this.itemJson.forEach(item=>{
				let json = {
					itemId:item.itemId,
					selected:Object.prototype.toString.call(item.selected)=='[object Number]'?item.selected:item.selected.join(","),
				}
				resJson.push(json);
			})
			let params = {
				voteId:this.form.voteId,
				result:JSON.stringify(resJson)
			}
			this.isSaveing = true;
			let res = await this.$API.info.vote.insertVoteResult.post(params);
			this.isSaveing = false;
			if (res.code == 200) {
				this.$emit('success')
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
			this.visible = false;
		},

		//显示
		open(model){
			this.model=model;
			this.visible = true;
			return this;
		},
		//表单注入数据
		setData(data){
			this.form.voteId=data.voteId
			this.form.title = data.title
			this.form.voteTypeName = data.voteTypeName
			this.form.sendDeptName = data.sendDeptName;
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.content=data.content
			this.form.attachId=data.attachId
			if(data.itemJson!='')
			{
				let arr = JSON.parse(data.itemJson);
				for(let i=0;i<arr.length;i++)
				{
					this.itemJson.push(arr[i]);
				}
			}
		},
		async getMyVoteById(data)
		{
			let res = await this.$API.info.vote.getMyVoteById.get({voteId:data});
			{
				if(res.code==200)
				{
					this.form.voteId=res.data.voteId
					this.form.title = res.data.title
					this.form.voteTypeName = res.data.voteTypeName
					this.form.sendDeptName = res.data.sendDeptName;
					this.form.beginTime = res.data.beginTime
					this.form.endTime = res.data.endTime
					this.form.content=res.data.content
					this.form.attachId=res.data.attachId
					if(res.data.itemJson!='')
					{
						let arr = JSON.parse(res.data.itemJson);
						for(let i=0;i<arr.length;i++)
						{
							this.itemJson.push(arr[i]);
						}
					}
				}else
				{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			}
		}
	}
}
</script>

<style>
</style>
